/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFPrincipalAdmin.java
 *
 * Created on 25/07/2011, 09:01:07
 */
package view.admin;

import control.Sessao;
import java.beans.PropertyVetoException;
import java.text.Normalizer.Form;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import model.Pessoa;
import util.StatusBar;
import util.Versao;
import view.IJIFSimpleFactory;
import view.JIFSimpleFactory;

/**
 *
 * @author Felipe
 */
public class JFPrincipalAdmin extends JFrame {

    private Pessoa usuarioLogado;
    private StatusBar statusBar;
     
    /** Creates new form JFPrincipalAdmin */
    public JFPrincipalAdmin() {
        this.usuarioLogado = Sessao.getInstance().getUsuarioLogado();
        initComponents();
        statusBar = new StatusBar();
        this.status.add(statusBar);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlCentro = new javax.swing.JPanel();
        pnlCentroNorte = new javax.swing.JPanel();
        pnlCentroSul = new javax.swing.JPanel();
        status = new javax.swing.JPanel();
        pnlCentroLeste = new javax.swing.JPanel();
        pnlCentroOeste = new javax.swing.JPanel();
        jDesktopPane = new javax.swing.JDesktopPane();
        jtbSuperior = new javax.swing.JToolBar();
        btnChamado = new javax.swing.JButton();
        btnPessoa = new javax.swing.JButton();
        btnCertficado = new javax.swing.JButton();
        btnEmail = new javax.swing.JButton();
        btnConfiguracao = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();
        jmbPrincipal = new javax.swing.JMenuBar();
        jmSistema = new javax.swing.JMenu();
        jmiConfiguracoes = new javax.swing.JMenuItem();
        jmiSair = new javax.swing.JMenuItem();
        jmCadastro = new javax.swing.JMenu();
        jmiPessoa = new javax.swing.JMenuItem();
        jmiCertificado = new javax.swing.JMenuItem();
        jmiEmail = new javax.swing.JMenuItem();
        jmChamado = new javax.swing.JMenu();
        jmiAbertura = new javax.swing.JMenuItem();
        jmiControle = new javax.swing.JMenuItem();
        jmAjuda = new javax.swing.JMenu();
        jmiSobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle(Versao.TITULO);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        pnlCentro.setLayout(new java.awt.BorderLayout());
        pnlCentro.add(pnlCentroNorte, java.awt.BorderLayout.PAGE_START);

        pnlCentroSul.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        pnlCentroSul.setLayout(new java.awt.BorderLayout());

        status.setLayout(new java.awt.BorderLayout());
        pnlCentroSul.add(status, java.awt.BorderLayout.EAST);

        pnlCentro.add(pnlCentroSul, java.awt.BorderLayout.PAGE_END);
        pnlCentro.add(pnlCentroLeste, java.awt.BorderLayout.LINE_END);
        pnlCentro.add(pnlCentroOeste, java.awt.BorderLayout.LINE_START);

        jDesktopPane.setBackground(new java.awt.Color(153, 153, 153));
        pnlCentro.add(jDesktopPane, java.awt.BorderLayout.CENTER);

        getContentPane().add(pnlCentro, java.awt.BorderLayout.CENTER);

        jtbSuperior.setRollover(true);

        btnChamado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i48x48/atendente3.png"))); // NOI18N
        btnChamado.setToolTipText("Chamado");
        btnChamado.setFocusable(false);
        btnChamado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnChamado.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnChamado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChamadoActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnChamado);

        btnPessoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i48x48/executivo1.png"))); // NOI18N
        btnPessoa.setToolTipText("Cadastro de usuários");
        btnPessoa.setFocusable(false);
        btnPessoa.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnPessoa.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnPessoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPessoaActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnPessoa);

        btnCertficado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i48x48/chave.png"))); // NOI18N
        btnCertficado.setToolTipText("Controle de validade dos certificados digitais");
        btnCertficado.setFocusable(false);
        btnCertficado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCertficado.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCertficado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCertficadoActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnCertficado);

        btnEmail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i48x48/email.png"))); // NOI18N
        btnEmail.setToolTipText("Controle das contas de e-mail");
        btnEmail.setFocusable(false);
        btnEmail.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEmail.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEmailActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnEmail);

        btnConfiguracao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i48x48/configuracoes.png"))); // NOI18N
        btnConfiguracao.setToolTipText("Configurações");
        btnConfiguracao.setFocusable(false);
        btnConfiguracao.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnConfiguracao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnConfiguracao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfiguracaoActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnConfiguracao);

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i48x48/sair.png"))); // NOI18N
        btnSair.setToolTipText("Sair do sistema");
        btnSair.setFocusable(false);
        btnSair.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSair.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnSair);

        getContentPane().add(jtbSuperior, java.awt.BorderLayout.PAGE_START);

        jmSistema.setText("Sistema");

        jmiConfiguracoes.setText("Configurações");
        jmiConfiguracoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiConfiguracoesActionPerformed(evt);
            }
        });
        jmSistema.add(jmiConfiguracoes);

        jmiSair.setText("Sair");
        jmiSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiSairActionPerformed(evt);
            }
        });
        jmSistema.add(jmiSair);

        jmbPrincipal.add(jmSistema);

        jmCadastro.setText("Cadastro");

        jmiPessoa.setText("Pessoa");
        jmiPessoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiPessoaActionPerformed(evt);
            }
        });
        jmCadastro.add(jmiPessoa);

        jmiCertificado.setText("Certificado Digital");
        jmiCertificado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiCertificadoActionPerformed(evt);
            }
        });
        jmCadastro.add(jmiCertificado);

        jmiEmail.setText("Email");
        jmiEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiEmailActionPerformed(evt);
            }
        });
        jmCadastro.add(jmiEmail);

        jmbPrincipal.add(jmCadastro);

        jmChamado.setText("Chamado");

        jmiAbertura.setText("Abertura");
        jmiAbertura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiAberturaActionPerformed(evt);
            }
        });
        jmChamado.add(jmiAbertura);

        jmiControle.setText("Controle");
        jmiControle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiControleActionPerformed(evt);
            }
        });
        jmChamado.add(jmiControle);

        jmbPrincipal.add(jmChamado);

        jmAjuda.setText("Ajuda");

        jmiSobre.setText("Sobre");
        jmiSobre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiSobreActionPerformed(evt);
            }
        });
        jmAjuda.add(jmiSobre);

        jmbPrincipal.add(jmAjuda);

        setJMenuBar(jmbPrincipal);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-900)/2, (screenSize.height-597)/2, 900, 597);
    }// </editor-fold>//GEN-END:initComponents

    private void btnPessoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPessoaActionPerformed
        abrirJIFPessoa();        
    }//GEN-LAST:event_btnPessoaActionPerformed
    
    private void jmiSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiSairActionPerformed
        fecharAplicacao();
    }//GEN-LAST:event_jmiSairActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        fecharAplicacao();
    }//GEN-LAST:event_btnSairActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        fecharAplicacao();
    }//GEN-LAST:event_formWindowClosing

    private void jmiEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiEmailActionPerformed
        abrirJIFEmail();
    }//GEN-LAST:event_jmiEmailActionPerformed

    private void jmiSobreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiSobreActionPerformed
        JOptionPane.showMessageDialog(null,
                Versao.SOBRE,                
                Versao.SOBRE_VER,
                JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jmiSobreActionPerformed

	private void btnChamadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChamadoActionPerformed
		abrirJIFChamadoAdmin();
	}//GEN-LAST:event_btnChamadoActionPerformed

    private void btnCertficadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCertficadoActionPerformed
		abrirJIFCertificado();
    }//GEN-LAST:event_btnCertficadoActionPerformed

    private void btnEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEmailActionPerformed
        abrirJIFEmail();
    }//GEN-LAST:event_btnEmailActionPerformed

    private void jmiPessoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiPessoaActionPerformed
        abrirJIFPessoa();
    }//GEN-LAST:event_jmiPessoaActionPerformed

    private void jmiCertificadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiCertificadoActionPerformed
        abrirJIFCertificado();
    }//GEN-LAST:event_jmiCertificadoActionPerformed

    private void jmiConfiguracoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiConfiguracoesActionPerformed
        abrirConfiguracao();
    }//GEN-LAST:event_jmiConfiguracoesActionPerformed

    private void btnConfiguracaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfiguracaoActionPerformed
        abrirConfiguracao();
    }//GEN-LAST:event_btnConfiguracaoActionPerformed

    private void jmiControleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiControleActionPerformed
        abrirJIFChamadoAdmin();               
	}//GEN-LAST:event_jmiControleActionPerformed

    private void jmiAberturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiAberturaActionPerformed
        JDAbrirChamado jdAbrirChamado = new JDAbrirChamado(this, true,
                this.usuarioLogado);
	}//GEN-LAST:event_jmiAberturaActionPerformed

    private void abrirConfiguracao() {
        JDConfiguracao configuracao = new JDConfiguracao(this, true);	
    }
       
    private void addFrame(Class classe) {
        boolean isCriado = true;

        // Se não houver nenhum JIF aberto ele cria um JIF
        if (jDesktopPane.getSelectedFrame() == null) {
            /* Através do simple factory eu reduzo o acoplamento do código
             *  melhorando a performance e manutenção */
            addJIF(JIFSimpleFactory.getJIF(classe));
            // Caso contrário..
        } else {
            JOptionPane.showMessageDialog(null, "Feche o controle atual e tente novamente.");
        }
        System.gc();
    }

    private void addJIF(JInternalFrame jif) {
        jDesktopPane.add(jif);
        try {
            jif.setMaximum(rootPaneCheckingEnabled);
            jif.setVisible(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
    
    private void abrirJIFPessoa() {
        addFrame(JIFPessoa.class);       
    }

    private void abrirJIFChamadoAdmin() {
        addFrame(JIFChamadoAdmin.class);
    }     
    
    private void abrirJIFCertificado() {
        addFrame(JIFCertificado.class);
    }

    private void abrirJIFEmail() {
        addFrame(JIFEmail.class);
    }

    private void fecharAplicacao() {
        int n = JOptionPane.showConfirmDialog(rootPane,
                "Deseja sair do sistema? ",
                "Sair do sistema",
                JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCertficado;
    private javax.swing.JButton btnChamado;
    private javax.swing.JButton btnConfiguracao;
    private javax.swing.JButton btnEmail;
    private javax.swing.JButton btnPessoa;
    private javax.swing.JButton btnSair;
    private javax.swing.JDesktopPane jDesktopPane;
    private javax.swing.JMenu jmAjuda;
    private javax.swing.JMenu jmCadastro;
    private javax.swing.JMenu jmChamado;
    private javax.swing.JMenu jmSistema;
    private javax.swing.JMenuBar jmbPrincipal;
    private javax.swing.JMenuItem jmiAbertura;
    private javax.swing.JMenuItem jmiCertificado;
    private javax.swing.JMenuItem jmiConfiguracoes;
    private javax.swing.JMenuItem jmiControle;
    private javax.swing.JMenuItem jmiEmail;
    private javax.swing.JMenuItem jmiPessoa;
    private javax.swing.JMenuItem jmiSair;
    private javax.swing.JMenuItem jmiSobre;
    private javax.swing.JToolBar jtbSuperior;
    private javax.swing.JPanel pnlCentro;
    private javax.swing.JPanel pnlCentroLeste;
    private javax.swing.JPanel pnlCentroNorte;
    private javax.swing.JPanel pnlCentroOeste;
    private javax.swing.JPanel pnlCentroSul;
    private javax.swing.JPanel status;
    // End of variables declaration//GEN-END:variables

    private void testarSwingWorker() {
        boolean isCriado = true;
        
        if (jDesktopPane.getSelectedFrame() == null) {
           
           addJIF(fazerParadinha());
            
        } else {
            JOptionPane.showMessageDialog(null, "Feche o controle atual e tente novamente.");
        }
        System.gc();
    }
    
    private JInternalFrame fazerParadinha() {
        Class classe = JIFChamadoAdmin.class;
        //addJIF(JIFSimpleFactory.getJIF(classe));
        IJIFSimpleFactory iFrame = null;
        JInternalFrame jif = (JInternalFrame)iFrame;
            try {
                iFrame = (IJIFSimpleFactory) classe.newInstance();            
            } catch (InstantiationException ex) {
                Logger.getLogger(JIFSimpleFactory.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(JIFSimpleFactory.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                jif = (JInternalFrame)iFrame;
            }
       return jif;
    }
    
      
        
   

}
